$(function () {
    $("body").on('click','#next-step',function () {//选择专业完成
        var subject = $("#customized-major-list li.dc-active");
        if(subject==null || subject.length<=0){
            $.toast('请选择一个专业');
            return;
        }
        var subjectId = subject.data('subject');
        saveUserSubjectData(subjectId);
    }).on('click','#customized-finished',function () {//科目选择完成
        var childList = $("#customized-subject-list li.active");
        if(childList==null || childList.length <=0){
            $.toast('请选择一个或多个科目');
            return;
        }
        var parentId = $("#parentId").val();
        var childIds = childList.map(function () {
            return $(this).data('subject');
        }).get().join(',');
        childIds = parentId + ","+childIds;
        saveUserSubjectData(childIds);
    }).on('click','#customized-major-list li',function () {//选择专业
        $(this).addClass('dc-active').siblings().removeClass('dc-active');;
    }).on('click','#customized-subject-list li',function () {//选择科目
        $(this).toggleClass('active');
    });

    /**
     * 保存用户的专业/科目数据
     * @param subjectIds
     */
    function saveUserSubjectData(subjectIds) {
        $.ajax({
            url: baseloaction + "/mobile/uc/academic/save/userAddSubject",
            data: {"subjectIds": subjectIds},
            type: "post",
            dataType: "json",
            success:function (result) {
                if(result.success==true){
                    //重新加载页面
                    window.location.href = baseloaction +'/mobile/uc/academic/myAcademic';
                }else{
                    $.toast(result.message);
                }
            }
        });
    }
});